package com.genius.reservation.main.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.main.entity.NoticeInformation;
import com.genius.reservation.main.entity.SwiperInformation;
import com.genius.reservation.mapper.NoticeMapper;
import com.genius.reservation.mapper.SwiperMapper;
import com.genius.reservation.tools.WxUtil;
import com.genius.reservation.tools.annotation.CurrentUser;
import com.genius.reservation.tools.annotation.webLog;
import com.genius.reservation.user.entity.UserInformation;
import com.genius.reservation.user.service.IUserInformationService;
import com.itextpdf.tool.xml.html.Body;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reservation/main")
@Api(tags = "主页功能控制器",value = "主页功能控制器")
public class MainPageController {
    @Autowired
    IUserInformationService userInformationService;
    @Autowired
    @Lazy
    SwiperMapper swiperMapper;


    @Autowired
    @Lazy
    NoticeMapper noticeMapper;


    @PostMapping("/login")
    @webLog
    @ApiOperation("登录")
    public ResponseRule login(@RequestParam String wxNumber) {
        System.out.println(wxNumber);
        if (ObjectUtil.isEmpty(wxNumber))
            return ResponseRule.no("登录失败");
        return userInformationService.loginUser(wxNumber);
    }

    @PostMapping("/modifyPassword")
    @ApiOperation("修改密码")
    public ResponseRule modifyPassword(@RequestBody JSONObject body) {
        if (JSONUtil.isNull(body))
            return ResponseRule.no("有必填项未填");
        return userInformationService.modifyPassword(body.getString("oldpassword"),body.getString("password"),body.getString("tpassword"),body.getString("userNumber"));
    }

    @PostMapping("/mainLogin")
    @webLog
    @ApiOperation("登录2")
    public ResponseRule mainLogin(@RequestBody JSONObject body) {
        String userNumber = body.getString("userNumber");
        String userPassword = body.getString("userPassword");
        String wxOpenId = body.getString("wxOpenId");
        String nickName = body.getString("nickName");
        String avatar = body.getString("avatar");
        String phone = body.getString("phone");
        UserInformation userInformation = userInformationService.login2(userNumber, userPassword);
        if (ObjectUtil.isEmpty(userInformation.getWxNumber())){
            return userInformationService.registerUser(userNumber,userPassword,wxOpenId,nickName,avatar,phone);
        }
        if (userInformation.getWxNumber()==wxOpenId) {
            return ResponseRule.ok(userInformation);
        }else {
            return ResponseRule.no("绑定信息错误,如有疑问请联系信息部");
        }
    }


    @GetMapping("/getSwiper")
    @webLog
    public ResponseRule getSwiper() {
        LambdaQueryWrapper<SwiperInformation> eq = swiperMapper.getWrapper().eq(SwiperInformation::getStatus, 0);
        List<SwiperInformation> swiperInformations = swiperMapper.selectList(eq);
        if (ObjectUtil.isEmpty(swiperInformations)){
            return ResponseRule.no();
        }
        ArrayList<Object> picList = new ArrayList<>();
        for (SwiperInformation swiperInformation : swiperInformations) {
            picList.add(swiperInformation.getUrl());
        }
        return ResponseRule.ok(picList);
    }


    @GetMapping("/getNotice")
    @webLog
    public ResponseRule getNotice() {
        LambdaQueryWrapper<NoticeInformation> eq = noticeMapper.getWrapper().eq(NoticeInformation::getStatus, 0);
        List<NoticeInformation> noticeInformations = noticeMapper.selectList(eq);
        if (ObjectUtil.isEmpty(noticeInformations)){
            return ResponseRule.no();
        }
        ArrayList<Object> contents = new ArrayList<>();
        for (NoticeInformation noticeInformation : noticeInformations) {
            contents.add(noticeInformation.getContent());
        }
        return ResponseRule.ok(contents);
    }

    @PostMapping("/getOpenId")
    @ApiOperation("获取openID")
    public ResponseRule getOpenId(@RequestBody JSONObject body){
        String code = body.getString("code");
        return WxUtil.getOpenId(code);
    }
//    @PostMapping(value = "/register")
//    @webLog
//    public ResponseRule register(@RequestParam Map<String,String> map) {
//        UserInformation userInformation = new UserInformation();
//        userInformation.setPhone(map.get("phone"));
//        userInformation.setUserRoleType(Integer.valueOf(map.get("userRoleType")));
//        userInformation.setWxNumber(map.get("wxNumber"));
//        userInformation.setNickName(map.get("nickName"));
//        userInformation.setAvatarUrl(map.get("avatarUrl"));
//        userInformation.setName(map.get("name"));
//        userInformation.setSupplierCode(map.get("supplierNumber"));
//        return userInformationService.registerUser(userInformation);
//    }


    @PostMapping(value = "/register")
    @webLog
    @ApiOperation("注册")
    public ResponseRule register(@RequestBody JSONObject body) {
        String userNumber = body.getString("userNumber");
        String userPassword = body.getString("userPassword");
        String wxOpenId = body.getString("wxOpenId");
        String nickName = body.getString("nickName");
        String avatar = body.getString("avatar");
        String phone = body.getString("phone");
        return userInformationService.registerUser(userNumber,userPassword,wxOpenId,nickName,avatar,phone);
    }

}
